/* 这是用户注册登录处理函数模块 */

const db = require('../../db')
const bcrypt = require('bcryptjs')
const jwt = require('jsonwebtoken')
const config = require('../../config')
const { result } = require('@hapi/joi/lib/base')

exports.getAllUsers = (req, res) => {
  const sql = 'select * from module3_users'
  db.query(sql, (err, results) => {
    if(err) return res.cc(err)
    res.send({
      status: 0,
      message: '数据返回成功',
      data: results,
    })
  })
}


exports.getAllBusinessmen = (req, res) => {
  const sql = 'select * from module3_users where is_businessmen = true'
  db.query(sql, (err, results) => {
    if(err) return res.cc(err)
    res.send({
      status: 0,
      message: '数据返回成功',
      data: results,
    })
  })
}

exports.getAllVip = (req, res) => {
  const sql = 'select * from module3_users where is_VIP = true'
  db.query(sql, (err, results) => {
    if(err) return res.cc(err)
    res.send({
      status: 0,
      message: '数据返回成功',
      data: results,
    })
  })
}

exports.getAllBlacklist = (req, res) => {
  const sql = 'select * from module3_users where is_blacklist = true'
  db.query(sql, (err, results) => {
    if(err) return res.cc(err)
    res.send({
      status: 0,
      message: '数据返回成功',
      data: results,
    })
  })
}

exports.getAllInspector = (req, res) => {
  const sql = 'select * from module3_users where is_inspector = true'
  db.query(sql, (err, results) => {
    if(err) return res.cc(err)
    res.send({
      status: 0,
      message: '数据返回成功',
      data: results,
    })
  })
}

exports.getAllAministrator = (req, res) => {
  const sql = 'select * from module3_users where is_administrator = true'
  db.query(sql, (err, results) => {
    if(err) return res.cc(err)
    res.send({
      status: 0,
      message: '数据返回成功',
      data: results,
    })
  })
}

exports.searchUsersID = (req, res) => {
  const name = req.params.value+'%';
  const sql = `select * from module3_users where user_id like ?`
  db.query(sql, [name], (err, results) => {
    if(err) return res.cc(err)
    res.send({
      status: 0,
      message: '数据返回成功',
      data: results,
    })
  })
}


exports.searchUsersName = (req, res) => {
  const name = req.params.value+'%';
  const sql = `select * from module3_users where user_name like ?`
  db.query(sql, [name], (err, results) => {
    if(err) return res.cc(err)
    res.send({
      status: 0,
      message: '数据返回成功',
      data: results,
    })
  })
}

exports.deleteUsers = (req, res) => {
  const userId = req.params.id; // 通过 params 传参
  const sql = " DELETE FROM module3_users WHERE user_id = ? ";

  db.query(sql, [userId], (err, results) => {
    if(err) return res.cc(err)
    if(results.affectedRows !== 1) return res.cc('删除失败，请稍后再试！')
    res.cc('删除成功！', 0)
  });
}

exports.updateIdentity = (req, res) => {
  console.log(req.params)
  const userId = req.params.id;
  isVIP=req.params.isVip;
  isBusinessmen=req.params.isBusinessmen;
  isBlacklist=req.params.isBlacklist;
  isInspector=req.params.isInspector;
  isAdministrator=req.params.isAdministrator;
  const sql = 'select * from module3_users where user_id = ?'
  db.query(sql, [userId], (err, results) => {
    if(err) return res.cc(err)
    if(results.length !== 1) return res.cc('更改失败，请稍后再试！')
    //console.log(req.params);
    const sql = 'UPDATE module3_users SET is_VIP = ?,is_businessmen = ?,is_blacklist = ?, is_inspector = ?,is_administrator = ? WHERE user_id = ?'
    db.query(sql,[isVIP,isBusinessmen,isBlacklist,isInspector,isAdministrator,userId], (err, results) => {
      if(err) return res.cc(err)
      if(results.affectedRows !== 1) return res.cc('更改失败，请稍后再试！')
      res.cc('更改成功！', 0)
    })
  })
}

exports.searchSpecificUsersID = (req, res) => {
  const name = req.params.value+'%';
  const sql = `select * from module3_users where user_id = ?`
  db.query(sql, [name], (err, results) => {
    if(err) return res.cc(err)
    res.send({
      status: 0,
      message: '数据返回成功',
      data: results,
    })
  })
}